package com.ymatou.iapi.notify.testcase;

import com.ymatou.iapi.notify.parameter.GetMessageListByTimelineBean;
import com.ymatou.iapi.notify.parameter.SendNotifyBean;
import com.ymatou.iapi.notify.parameter.emu.MessageIsNew;
import com.ymatou.iapi.notify.service.GetMessageListByTimelineCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.NotifyCallService;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.utils.YMTDateUtil;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
*获取通知列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetMessageListByTimeline{
	private static GetMessageListByTimelineBean getmessagelistbytimelineBean;
	private static GetMessageListByTimelineCall getmessagelistbytimelineCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取通知列表");
	}

	@Before
	public void caseUp() {
		getmessagelistbytimelineBean=new GetMessageListByTimelineBean();
		getmessagelistbytimelineCall=new GetMessageListByTimelineCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getmessagelistbytimelineCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMessageListByTimeline_01() {
		Logger.start(true,"获取通知列表，单个通知");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			SendNotifyBean sendNotifyBean = NotifyCallService.initSendNotify(30156393,userId);
			NotifyCallService.SendNotify(sendNotifyBean);
			getmessagelistbytimelineBean.setFromUserId(sendNotifyBean.getNotifySenderUserId());
			getmessagelistbytimelineBean.setToUserId(userId);
			getmessagelistbytimelineBean.setIsNew(MessageIsNew.No);
			getmessagelistbytimelineBean.setLongTimeline(YMTDateUtil.getDate().getTime());
			getmessagelistbytimelineBean.setMessageCount("10");
			getmessagelistbytimelineBean.setNotClearUnReadCount(true);
			getmessagelistbytimelineCall.setData(getmessagelistbytimelineBean);
			getmessagelistbytimelineCall.callService();			
			Logger.verifyEquals("Success",getmessagelistbytimelineCall.getString("Status"),"验证Status");
			verifyDB(getmessagelistbytimelineBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMessageListByTimeline_02() {
		Logger.start(true,"获取通知列表，多个通知");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			SendNotifyBean sendNotifyBean = NotifyCallService.initSendNotify(30156393,userId);
			NotifyCallService.SendNotify(sendNotifyBean);
			NotifyCallService.SendNotify(sendNotifyBean);
			getmessagelistbytimelineBean.setFromUserId(sendNotifyBean.getNotifySenderUserId());
			getmessagelistbytimelineBean.setToUserId(userId);
			getmessagelistbytimelineBean.setIsNew(MessageIsNew.No);
			getmessagelistbytimelineBean.setLongTimeline(YMTDateUtil.getDate().getTime());
			getmessagelistbytimelineBean.setMessageCount("10");
			getmessagelistbytimelineBean.setNotClearUnReadCount(true);
			getmessagelistbytimelineCall.setData(getmessagelistbytimelineBean);
			getmessagelistbytimelineCall.callService();
			Logger.verifyEquals("Success",getmessagelistbytimelineCall.getString("Status"),"验证Status");
			verifyDB(getmessagelistbytimelineBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMessageListByTimeline_03() {
		Logger.start(true,"获取通知列表，通知号不存在");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			SendNotifyBean sendNotifyBean = NotifyCallService.initSendNotify(123344,userId);
			NotifyCallService.SendNotify(sendNotifyBean);
			NotifyCallService.SendNotify(sendNotifyBean);
			getmessagelistbytimelineBean.setFromUserId(sendNotifyBean.getNotifySenderUserId());
			getmessagelistbytimelineBean.setToUserId(userId);
			getmessagelistbytimelineBean.setIsNew(MessageIsNew.No);
			getmessagelistbytimelineBean.setLongTimeline(YMTDateUtil.getDate().getTime());
			getmessagelistbytimelineBean.setMessageCount("10");
			getmessagelistbytimelineBean.setNotClearUnReadCount(true);
			getmessagelistbytimelineCall.setData(getmessagelistbytimelineBean);
			getmessagelistbytimelineCall.callService();
			Logger.verifyEquals("Success",getmessagelistbytimelineCall.getString("Status"),"验证Status");
			Logger.verifyEquals(true,getmessagelistbytimelineCall.getResults().size()==0,"验证返回结果为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMessageListByTimeline_04() {
		Logger.start(true,"获取通知列表，接收用户不存在");
		try {
			int userId = 465728477;
			SendNotifyBean sendNotifyBean = NotifyCallService.initSendNotify(30156393,userId);
			NotifyCallService.SendNotify(sendNotifyBean);
			NotifyCallService.SendNotify(sendNotifyBean);
			getmessagelistbytimelineBean.setFromUserId(sendNotifyBean.getNotifySenderUserId());
			getmessagelistbytimelineBean.setToUserId(userId);
			getmessagelistbytimelineBean.setIsNew(MessageIsNew.No);
			getmessagelistbytimelineBean.setLongTimeline(YMTDateUtil.getDate().getTime());
			getmessagelistbytimelineBean.setMessageCount("10");
			getmessagelistbytimelineBean.setNotClearUnReadCount(true);
			getmessagelistbytimelineCall.setData(getmessagelistbytimelineBean);
			getmessagelistbytimelineCall.callService();
			Logger.verifyEquals("BussinessError",getmessagelistbytimelineCall.getString("Status"),"验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMessageListByTimeline_05() {
		Logger.start(true,"获取通知列表，传入时间往后正序");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			SendNotifyBean sendNotifyBean = NotifyCallService.initSendNotify(30156393,userId);

			getmessagelistbytimelineBean.setFromUserId(sendNotifyBean.getNotifySenderUserId());
			getmessagelistbytimelineBean.setToUserId(userId);
			getmessagelistbytimelineBean.setIsNew(MessageIsNew.Yes);
			getmessagelistbytimelineBean.setLongTimeline(YMTDateUtil.getDate().getTime());
			getmessagelistbytimelineBean.setMessageCount("10");
			getmessagelistbytimelineBean.setNotClearUnReadCount(true);
			Thread.sleep(5000);
			NotifyCallService.SendNotify(sendNotifyBean);
			getmessagelistbytimelineCall.setData(getmessagelistbytimelineBean);
			getmessagelistbytimelineCall.callService();
			Logger.verifyEquals("Success",getmessagelistbytimelineCall.getString("Status"),"验证Status");
			verifyDB(getmessagelistbytimelineBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	public void verifyDB(GetMessageListByTimelineBean getMessageListBean) throws IOException {
		String UserLogoUrl = JUserCallService.GetLogoUrl(getMessageListBean.getToUserId());
		String UserLoginId = JUserCallService.GetUserInfo(getMessageListBean.getToUserId()).getLoginId();
		List<Map> mapList = new ImDefaultWapper().selectImNotifyByToUserId(getMessageListBean.getToUserId());
		for (Map map:mapList
			 ) {
			String notifyId = map.get("NotifyId").toString();
			String messageId = map.get("MsgFromId")+"_"+map.get("MsgToId")+"_"+notifyId;
			getmessagelistbytimelineCall.getResults().stream().filter(x->x.getMessageId().equals(messageId)).forEach(p->{
				Logger.verifyEquals(p.getMsgFromId(),Integer.parseInt(map.get("MsgFromId").toString()),"验证通知号");
				Logger.verifyEquals(p.getUserLoginId(),UserLoginId,"验证用户id");
				Logger.verifyEquals(p.getUserLogoUrl(),UserLogoUrl,"验证用户头像");
				Logger.verifyEquals(p.getMessage(),map.get("Message").toString(),"验证Message");
				AssertService.assertTime(YMTDateUtil.parseDate(p.getPostTime(),YMTDateUtil.YMDHMS),YMTDateUtil.getDate(map.get("PostTime").toString(),YMTDateUtil.YMDHMS),5,"验证创建时间");
				String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(p.getLongPostTime()));
				Date date = YMTDateUtil.getDate(time,YMTDateUtil.YMDHMS);
				AssertService.assertTime(date,YMTDateUtil.getDate(map.get("PostTime").toString(),YMTDateUtil.YMDHMS),5,"验证创建时间长整型");
				Logger.verifyEquals(p.getIsRead().toString(),Integer.parseInt(map.get("IsRead").toString()) == 0?"UnRead":"Read","验证是否未读");
			});
		}
	}
}